package com.per.yyyFrame.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.web.servlet.view.AbstractUrlBasedView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/2/8
 * Time: 9:50
 * Description:
 */
public class JsonView<T> extends AbstractUrlBasedView {
  private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
  private T data;

  public JsonView(T data) {
    super.setUrl("");
    this.data = data;
  }

  protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    String res = "";
//    if (UseDateFormatImportSelector.enabledFormat) {
//      JSON.DEFFAULT_DATE_FORMAT = UseDateFormatImportSelector.format;
      res = JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat});
//    } else {
      res = JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
 //   }

    httpServletResponse.setContentType("application/json;charset=UTF-8");
    httpServletResponse.getWriter().write(res);
    httpServletResponse.getWriter().close();
  }
}

